<?php
namespace app\admin\controller;

use app\admin\logic\LoginLogic;
use app\common\model\AdminModel;
use think\Controller;
use think\facade\Cache;
use think\facade\Request;
class Login extends Controller{

    private $loginService;

    public function __construct(){
        $this->loginService = new loginLogic();
    }

    public function clear(){
        Cache::clear();
        return view();
    }

    /**
     * 管理后台登录页面
     * @return mixed
     */
    public function index(){
        return view();
    }

    public function info(){
        $count[] = db('site_team')->where('status','=',0)->count();
        $count[] = db('volunteer')->where('status','=',0)->count();
        $count[] = db('volunteer')->where('o_status','=',0)->count();
        $count[] = db('practice')->where('status','=',0)->count();
        $count[] = db('user')->count();
        $count[] = db('article')->count();
        $count[] = db('practice')->count();
        $count[] = db('forum')->count();
        $this->assign('count',$count);
        $site_list = db('site_team')->limit(6)->order('number desc')->select();
        $num = $site_list[0]['number'];
        foreach($site_list as $k=>$v){
            $site_list[$k]['bai'] = round($v['number']/$num*100);
        }
        $this->assign('site_list',$site_list);
        $wish[] = db('wish')->where('status','=',0)->count();
        $wish[] = db('wish')->where('status','=',1)->where('order_status','=',0)->count();
        $wish[] = db('wish')->where('status','=',1)->where('order_status','=',1)->count();
        $this->assign('wish',$wish);
        $num = db('volunteer')->count();
        $arr = [];
        for($i=0;$i<strlen($num);$i++){
            $arr[] = substr($num,$i,1);
        }
        $this->assign('num', $arr);
        $time = time();
        $userx = '';
        $usery = '';
        $volunteery = '';
        for($i=0;$i<7;$i++){
            $t = $time+$i*86400;
            $s_time = strtotime(date('Y-m-d',$t));
            $userx.=','.date('m/d',$t);
            $usery.=','.db('user')->where('create_time','>',$s_time)->where('create_time','>',$s_time)->count();
            $volunteery.=','.db('volunteer')->where('create_time','>',$s_time)->where('create_time','>',$s_time)->count();;
        }
        $this->assign('userx', ltrim($userx,','));
        $this->assign('usery', ltrim($usery,','));
        $this->assign('volunteery', ltrim($volunteery,','));
        return $this->fetch();
    }

    /**
     * 管理后台登录逻辑
     * @return \think\response\Json
     */
	public function doLogin(){
        $data = Request::post();
        $res = $this->loginService->login($data,Request::ip());
        return json($res);
	}

    /**
     * 管理后台登出
     */
	public function logout(){
        $this->loginService->logout();
        return $this->redirect('Login/index');
    }
}
